﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
    public class PlayerState : MonoBehaviour
    {
        public static PlayerState Instance;
        private float curTime;

        public enum State
        {
            IDLE,
            RUN
        }

        private void Start()
        {
            Instance = this;       
        }

        public State state = State.IDLE;

        public PlayerState.State SetState(State state, Transform target, int time)
        {
            if (state == State.IDLE)
            {
                //PlayerControl.Instance.ani.SetTrigger("Idle1");
                InvokeRepeating("ChangeIdle", 2, 5);
            }
            else if (state == State.RUN)
            {
                GameObject.Find("XiaoQi").transform.LookAt(target);
                GameObject.Find("Role_XiaoQi").GetComponent<Animator>().SetTrigger("Run");
                GameObject.Find("XiaoQi").transform.DOLocalMove(target.localPosition, time).SetEase(Ease.Linear).OnComplete(() =>
                {
                //到达目的地
                ArriveDestination();
                });
            }
            state = state;
            return state;
        }

        void ChangeIdle()
        {
            int a = Random.Range(2, 4);
            GameObject.Find("Role_XiaoQi").GetComponent<Animator>().SetTrigger("Idle" + a);
        }

        void ArriveDestination()
        {
            PlayerControl.Instance.ani.SetTrigger("Idle1");
            //朝前
            PlayerControl.Instance.role.transform.DOLocalRotate(new Vector3(0, -180, 0), 0.5f);
        }
    }
